package com.cocostudio.erpapp.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class CustomAdapter<T> extends BaseAdapter {
    private Context mContext;
    private ArrayList<T> mData;
    private LayoutInflater mLayoutInflater;

    public CustomAdapter(Context context, ArrayList<T> data) {
        mContext = context;
        mData = data;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void refresh(ArrayList<T> newData) {
        mData = newData;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public T getItem(int position) {
        return mData == null ? null : mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(getItemLayout(), null);
        }
        onConfigureView(mContext, convertView, getItem(position));
        return convertView;
    }

    protected abstract void onConfigureView(Context context, View convertView, T data);

    protected abstract int getItemLayout();
}
